ostree-prepare-root: Cope with /proc not being mounted
authorWilliam Manley <will@williammanley.net>
Mon, 18 Jul 2016 16:44:19 +0000 (17:44 +0100)
committerAtomic Bot <atomic-devel@projectatomic.io>
Tue, 2 Aug 2016 19:07:25 +0000 (19:07 +0000)
When trying to read kernel command-line.

Closes: #403
Approved by: cgwalters

src/switchroot/ostree-prepare-root.c

index a233007ef6e894a826427451d2ba299c3c10175b..77a084ade03b14b65bd2002a829926829c436339 100644 (file)
 #include "ostree-mount-util.h"
 
 static char *
-parse_ostree_cmdline (void)
+read_proc_cmdline (void)
 {
   FILE *f = fopen("/proc/cmdline", "r");
   char *cmdline = NULL;
-  const char *iter;
-  char *ret = NULL;
   size_t len;
 
   if (!f)
-    return NULL;
+    goto out;
+
   /* Note that /proc/cmdline will not end in a newline, so getline
    * will fail unelss we provide a length.
    */
   if (getline (&cmdline, &len, f) < 0)
-    return NULL;
+    goto out;
   /* ... but the length will be the size of the malloc buffer, not
    * strlen().  Fix that.
    */
@@ -67,6 +66,47 @@ parse_ostree_cmdline (void)
 
   if (cmdline[len-1] == '\n')
     cmdline[len-1] = '\0';
+out:
+  if (f)
+    fclose (f);
+  return cmdline;
+}
+
+static char *
+parse_ostree_cmdline (void)
+{
+  char *cmdline = NULL;
+  const char *iter;
+  char *ret = NULL;
+  int tmp_errno;
+
+  cmdline = read_proc_cmdline ();
+  if (!cmdline)
+    {
+      // Mount proc
+      if (mount ("proc", "/proc", "proc", 0, NULL) < 0)
+        {
+          perrorv ("failed to mount proc on /proc: ");
+          exit (EXIT_FAILURE);
+        }
+
+      cmdline = read_proc_cmdline ();
+      tmp_errno = errno;
+
+      /* Leave the filesystem in the state that we found it: */
+      if (umount ("/proc"))
+        {
+          perrorv ("failed to umount proc from /proc: ");
+          exit (EXIT_FAILURE);
+        }
+
+      errno = tmp_errno;
+      if (!cmdline)
+        {
+          perrorv ("failed to read /proc/cmdline: ");
+          exit (EXIT_FAILURE);
+        }
+    }
 
   iter = cmdline;
   while (iter != NULL)